//
//  AddWorkExpViewController.m
//  CYWFrame
//
//  Created by 于顺燊 on 2018/9/17.
//  Copyright © 2018年 Macx. All rights reserved.
//

#import "AddWorkExpViewController.h"
#import "WSDatePickerView.h"

@interface AddWorkExpViewController ()<UITextFieldDelegate, UITextViewDelegate>

@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) NSString *startDate;
@property (strong, nonatomic) NSString *endDate;

@end

@implementation AddWorkExpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	self.view.backgroundColor = UISeparatedColorRGB;
	[self createUI];
    // Do any additional setup after loading the view.
}

- (void)createUI {
	if (self.type == 1) {
		UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
		[deleteBtn addTarget:self action:@selector(deleteWorkExpInfo)forControlEvents:UIControlEventTouchUpInside];
		[deleteBtn setTitle:@"删除经历" forState:UIControlStateNormal];
		[deleteBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
		[deleteBtn setTintColor:kWhiteColor];
		UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:deleteBtn];
		self.navigationItem.rightBarButtonItem = rightItem;
		self.navigationItem.title = @"编辑工作经历";
	}
	else {
		self.navigationItem.title = @"添加工作经历";
	}
	
	_scrollView.backgroundColor = UISeparatedColorRGB;
	_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
	[self.view addSubview:_scrollView];
	NSArray *arr = [NSArray arrayWithObjects:@"   开始时间", @"   结束时间", @"   公司名称", @"   职位名称", @"   工作描述", nil];
	
	CGSize size = [LZCTools textSizeWithText:@"字数" font:kUHSystemFontWithSize(15)];
	
	for (int i = 0; i < arr.count; i++) {
		UILabel *label = [[UILabel alloc] init];
		[_scrollView addSubview:label];
		label = [self createLabel:label text:arr[i]];
		label.tag = 1+i;
	}
	
	//工作开始时间
	UILabel *startDateL = [self.view viewWithTag:1];
	startDateL.frame = CGRectMake(0, 10, SCREEN_WIDTH, size.height+30);
	
	UITextField *startDateF = [[UITextField alloc] initWithFrame:CGRectMake(30+size.width, startDateL.y, SCREEN_WIDTH-(50+size.width), size.height+30)];
	[_scrollView addSubview:startDateF];
	startDateF.delegate = self;
	startDateF.textColor = kGrayColor;
	startDateF.clearButtonMode = UITextFieldViewModeWhileEditing;
	startDateF.font = kUHSystemFontWithSize(15);
	startDateF.textAlignment = NSTextAlignmentRight;
	startDateF.rightViewMode = UITextFieldViewModeAlways;
	startDateF.tag = 11;
	UIImageView *imgV1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, size.height/2, size.height)];
	startDateF.rightView = imgV1;
	imgV1.image = IMAGE(@"arrow_right_grey");
	NSString *startTimeStr = self.info[@"start_time"];
	self.startDate = startTimeStr;
	if (startTimeStr.length >= 8) {
		NSString *yearStr = [startTimeStr substringWithRange:NSMakeRange(0, 4)];
		NSString *monthStr = [startTimeStr substringWithRange:NSMakeRange(4, 2)];
		NSString *dayStr = [startTimeStr substringWithRange:NSMakeRange(6, 2)];
		startDateF.text = [NSString stringWithFormat:@"%@年%@月%@日", yearStr, monthStr, dayStr];
	}
	UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(startDateF.x, startDateF.y, startDateF.width, startDateF.height)];
	[_scrollView addSubview:view1];
	[view1 addActionWithblock:^{
		NSLog(@"选择开始时间");
		[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
		// 显示日期选择
		WSDatePickerView *datepicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowYearMonthDay CompleteBlock:^(NSDate *selectDate) {
			
			//            NSString *date = [selectDate stringWithFormat:@"yyyy-MM-dd"];
			NSString *date = [NSString stringWithFormat:@"%ld年%ld月%ld日",selectDate.year,selectDate.month,selectDate.day];
			startDateF.text = date;
			self.startDate = [NSString stringWithFormat:@"%ld%02ld%02ld",selectDate.year,selectDate.month,selectDate.day];
		}];
		
		NSDateFormatter *minDateFormater = [[NSDateFormatter alloc] init];
		[minDateFormater setDateFormat:@"yyyy-MM-dd"];
		NSDate *scrollToDate = [minDateFormater dateFromString:@"1930-1-1"];
		datepicker.minLimitDate = scrollToDate;
		datepicker.maxLimitDate = [NSDate date];
		[datepicker show];
	}];
	
	//结束时间
	UILabel *endDateL = [self.view viewWithTag:2];
	endDateL.frame = CGRectMake(0, startDateL.y+startDateL.height+1, SCREEN_WIDTH, size.height+30);
	
	UITextField *endDateF = [[UITextField alloc] initWithFrame:CGRectMake(30+size.width, endDateL.y, SCREEN_WIDTH-(50+size.width), size.height+30)];
	[_scrollView addSubview:endDateF];
	endDateF.delegate = self;
	endDateF.textColor = kGrayColor;
	endDateF.clearButtonMode = UITextFieldViewModeWhileEditing;
	endDateF.font = kUHSystemFontWithSize(15);
	endDateF.textAlignment = NSTextAlignmentRight;
	endDateF.rightViewMode = UITextFieldViewModeAlways;
	endDateF.tag = 12;
	UIImageView *imgV2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, size.height/2, size.height)];
	endDateF.rightView = imgV2;
	imgV2.image = IMAGE(@"arrow_right_grey");
	NSString *endTimeStr = self.info[@"end_time"];
	self.endDate = endTimeStr;
	NSLog(@"教育经历结束时间%@",endTimeStr);
	if (endTimeStr.length >= 8) {
		NSString *yearStr2 = [endTimeStr substringWithRange:NSMakeRange(0, 4)];
		NSString *monthStr2 = [endTimeStr substringWithRange:NSMakeRange(4, 2)];
		NSString *dayStr2 = [endTimeStr substringWithRange:NSMakeRange(6, 2)];
		endDateF.text = [NSString stringWithFormat:@"%@年%@月%@日", yearStr2, monthStr2, dayStr2];
	}
	UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(endDateF.x, endDateF.y, endDateF.width, endDateF.height)];
	[_scrollView addSubview:view2];
	[view2 addActionWithblock:^{
		NSLog(@"选择结束时间");
		[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
		// 显示日期选择
		WSDatePickerView *datepicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowYearMonthDay CompleteBlock:^(NSDate *selectDate) {
			
			//            NSString *date = [selectDate stringWithFormat:@"yyyy-MM-dd"];
			NSString *date = [NSString stringWithFormat:@"%ld年%ld月%ld日",selectDate.year,selectDate.month,selectDate.day];
			endDateF.text = date;
			self.endDate = [NSString stringWithFormat:@"%ld%02ld%02ld",selectDate.year,selectDate.month,selectDate.day];
		}];
		
		NSDateFormatter *minDateFormater = [[NSDateFormatter alloc] init];
		[minDateFormater setDateFormat:@"yyyy-MM-dd"];
		NSDate *scrollToDate = [minDateFormater dateFromString:@"1930-1-1"];
		datepicker.minLimitDate = scrollToDate;
		datepicker.maxLimitDate = [NSDate date];
		[datepicker show];
	}];
	
	//公司名称
	UILabel *companyNameL = [self.view viewWithTag:3];
	companyNameL.frame = CGRectMake(0, endDateL.y+endDateL.height+1, SCREEN_WIDTH, size.height+30);
	
	UITextField *companyNameF = [[UITextField alloc] initWithFrame:CGRectMake(30+size.width, companyNameL.y, SCREEN_WIDTH-(50+size.width), size.height+30)];
	[_scrollView addSubview:companyNameF];
	companyNameF.delegate = self;
	companyNameF.textColor = kGrayColor;
	companyNameF.clearButtonMode = UITextFieldViewModeWhileEditing;
	companyNameF.font = kUHSystemFontWithSize(15);
	companyNameF.textAlignment = NSTextAlignmentRight;
	companyNameF.rightViewMode = UITextFieldViewModeAlways;
	companyNameF.tag = 13;
	companyNameF.text = self.info[@"company_name"];
	UIImageView *imgV3 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, size.height/2, size.height)];
	companyNameF.rightView = imgV3;
	imgV3.image = IMAGE(@"arrow_right_grey");
	
	
	//职位名称
	UILabel *jobNameL = [self.view viewWithTag:4];
	jobNameL.frame = CGRectMake(0, companyNameL.y+companyNameL.height+1, SCREEN_WIDTH, size.height+30);
	
	UITextField *jobNameF = [[UITextField alloc] initWithFrame:CGRectMake(30+size.width, jobNameL.y, SCREEN_WIDTH-(50+size.width), size.height+30)];
	[_scrollView addSubview:jobNameF];
	jobNameF.delegate = self;
	jobNameF.textColor = kGrayColor;
	jobNameF.clearButtonMode = UITextFieldViewModeWhileEditing;
	jobNameF.font = kUHSystemFontWithSize(15);
	jobNameF.textAlignment = NSTextAlignmentRight;
	jobNameF.rightViewMode = UITextFieldViewModeAlways;
	jobNameF.tag = 14;
	jobNameF.text = self.info[@"position_name"];
	UIImageView *imgV4 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, size.height/2, size.height)];
	jobNameF.rightView = imgV4;
	imgV4.image = IMAGE(@"arrow_right_grey");
	
	//工作描述
	UILabel *jobDescL = [self.view viewWithTag:5];
	jobDescL.frame = CGRectMake(0, jobNameL.y+jobNameL.height+1, SCREEN_WIDTH, size.height+30);
	
	UITextView *jobDescF = [[UITextView alloc] init];
	[_scrollView addSubview:jobDescF];
	NSString *jobDesc = self.info[@"desc"];;
	jobDescF.text = jobDesc;
	jobDescF.textColor = [UIColor lightGrayColor];
	jobDescF.font = kUHSystemFontWithSize(14);
	jobDescF.delegate = self;
	//    signTV.adjustsFontForContentSizeCategory = YES;
	[jobDescF scrollRangeToVisible:NSMakeRange(0,1)];
	CGSize signSize = [jobDescF sizeThatFits:CGSizeMake(SCREEN_WIDTH-20-SW(20), MAXFLOAT)];
	jobDescF.frame = CGRectMake(0, jobDescL.y+jobDescL.height+1, SCREEN_WIDTH, 15+signSize.height*2);
	jobDescF.textContainerInset = UIEdgeInsetsMake(15, SW(20), 15, 20);//设置页边
	jobDescF.tag = 15;
	
	//保存按钮
	UIButton *saveBtn = [[UIButton alloc] initWithFrame:CGRectMake( 30, jobDescF.y+jobDescF.height+20,  SCREEN_WIDTH-30*2, 40)];
	saveBtn.layer.cornerRadius = saveBtn.height/4;
	[saveBtn setTitle:@"保存" forState:UIControlStateNormal];
	[saveBtn setBackgroundColor:UIMainColorRGB];
	[saveBtn addTarget:self action:@selector(addJobExp) forControlEvents:UIControlEventTouchUpInside];
	[_scrollView addSubview:saveBtn];
	
	_scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, saveBtn.y+saveBtn.height+20);
}

- (UILabel *)createLabel:(UILabel *)label text:(NSString *)text {
	
	label.textColor = [UIColor grayColor];
	label.text = text;
	label.font = kUHSystemFontWithSize(15);
	label.backgroundColor = kWhiteColor;
	return label;
}

- (void)addJobExp
{
	UITextField *startTF = (UITextField *)[self.view viewWithTag:11];
	UITextField *endTF = (UITextField *)[self.view viewWithTag:12];
	UITextField *companyNameTF = (UITextField *)[self.view viewWithTag:13];
	UITextField *jobNameTF = (UITextField *)[self.view viewWithTag:14];
	UITextField *jobDescTF = (UITextField *)[self.view viewWithTag:15];
	
	NSArray *arr = @[startTF, endTF, companyNameTF, jobNameTF, jobDescTF];
	for(UITextField *tf in arr)
	{
		if ([NSString isEmpty:tf.text]) {
			[self presentViewController:[LZCTools alertMessage:@"信息不能为空，请填写完所有信息后再次提交"] animated:YES completion:nil];
			return;
		}
	}
	
	if (self.type == 0) {
		NSDictionary *param = @{
								@"resume_id":self.resumeId,
								@"company_name":companyNameTF.text,
								@"start_time":self.startDate,
								@"end_time":self.endDate,
								@"position_name":jobNameTF.text,
								@"desc":jobDescTF.text
								};
		[[NetworkTool sharedTool] requestWithName:@"添加工作经历" URLString:APIAddWorkExp parameters:param method:@"POST" callBack:^(id responseObject) {
			NSString *resultState = responseObject[@"data"][@"status"];
			if ([resultState integerValue] == 1) {
				UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"添加成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
				[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
					[self.navigationController popViewControllerAnimated:YES];
				}]];
				[self presentViewController:alert animated:YES completion:nil];
			}
		}];
	}
	else {
		NSDictionary *param = @{
								@"id":self.info[@"id"],
								@"resume_id":self.resumeId,
								@"company_name":companyNameTF.text,
								@"start_time":self.startDate,
								@"end_time":self.endDate,
								@"position_name":jobNameTF.text,
								@"desc":jobDescTF.text
								};
		[[NetworkTool sharedTool] requestWithName:@"编辑工作经历" URLString:APIUpdateWorkExp parameters:param method:@"POST" callBack:^(id responseObject) {
			NSString *resultState = responseObject[@"data"][@"status"];
			UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"编辑成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
			[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
				[self.navigationController popViewControllerAnimated:YES];
			}]];
			[self presentViewController:alert animated:YES completion:nil];
		}];
	}
	
}

- (void)deleteWorkExpInfo {
	UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定删除？" message:nil preferredStyle:UIAlertControllerStyleAlert];
	[alert addAction:[UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
	}]];
	[alert addAction:[UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
		NSDictionary *param = @{
								@"resume_id":self.resumeId,
								@"del_id":self.info[@"id"],
								@"type":@"1"
								};
		[[NetworkTool sharedTool] requestWithName:@"删除工作经历" URLString:APIDeleteExpInfo parameters:param method:@"POST" callBack:^(id responseObject) {
			UIAlertController *actionSuccessAlert = [UIAlertController alertControllerWithTitle:@"删除成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
			[actionSuccessAlert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
				[self.navigationController popViewControllerAnimated:YES];
			}]];
			[self presentViewController:actionSuccessAlert animated:YES completion:nil];
		}];
	}]];
	[self presentViewController:alert animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
